home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / utility / pl4019ax.zip / ASSERT.PL < prev    next >
Text File  |  1991-11-13  |  1KB  |  53 lines

  1. # assert.pl
  2. # tchrist@convex.com (Tom Christiansen)
  3. # Usage:
  4. #     &assert('@x > @y');
  5. #     &assert('$var > 10', $var, $othervar, @various_info);
  6. # That is, if the first expression evals false, we blow up.  The
  7. # rest of the args, if any, are nice to know because they will
  8. # be printed out by &panic, which is just the stack-backtrace
  9. # routine shamelessly borrowed from the perl debugger.
  10.  
  11. sub assert {
  12.     &panic("ASSERTION BOTCHED: $_[0]",$@) unless eval $_[0];
  13.  
  14. sub panic {
  15.     select(STDERR);
  16.  
  17.     print "\npanic: @_\n";
  18.  
  19.     exit 1 if $] <= 4.003;  # caller broken
  20.  
  21.     # stack traceback gratefully borrowed from perl debugger
  22.  
  23.     local($i,$_);
  24.     local($p,$f,$l,$s,$h,$a,@a,@sub);
  25.     for ($i = 0; ($p,$f,$l,$s,$h,$w) = caller($i); $i++) {
  26.     @a = @DB'args;
  27.     for (@a) {
  28.         if (/^StB\000/ && length($_) == length($_main{'_main'})) {
  29.         $_ = sprintf("%s",$_);
  30.         }
  31.         else {
  32.         s/'/\\'/g;
  33.         s/([^\0]*)/'$1'/ unless /^-?[\d.]+$/;
  34.         s/([\200-\377])/sprintf("M-%c",ord($1)&0177)/eg;
  35.         s/([\0-\37\177])/sprintf("^%c",ord($1)^64)/eg;
  36.         }
  37.     }
  38.     $w = $w ? '@ = ' : '$ = ';
  39.     $a = $h ? '(' . join(', ', @a) . ')' : '';
  40.     push(@sub, "$w&$s$a from file $f line $l\n");
  41.     }
  42.     for ($i=0; $i <= $#sub; $i++) {
  43.     print $sub[$i];
  44.     }
  45.     exit 1;
  46.  
  47. 1;
  48.